package com.guanjia.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

@Controller
@RequestMapping("/rating")
public class RatingController {

    @RequestMapping("/getRating")
    public String getRating(){
        return "rating";
    }
    @RequestMapping("/getRating2")
    public String getRating2(){
        return "rating2";
    }

    @ResponseBody
    @RequestMapping(value = "savePic",method = {RequestMethod.POST})
    public String savePic(HttpServletRequest request) throws IOException {
         System.out.println("ok");
        String json=request.getParameter("name");
         System.out.println(json);
        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null)
                {
                    String path="E:/picall/a3/"+file.getOriginalFilename();

                    //上传
                    file.transferTo(new File(path));
                }

            }

        }
        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "savePic2",method = {RequestMethod.POST})
    public String savePic2(@RequestParam("img1") MultipartFile[] multipartfiles) throws IOException {
        System.out.println("savePic2");
        if(multipartfiles != null && multipartfiles.length>0){
            for(MultipartFile item : multipartfiles)
            { String fileName = item.getOriginalFilename();
                //获取文件名
                String filePath = "E:/picall/a3/" ;
                //自定义上传路径
                File file = new File(filePath,fileName);
                if(!file.exists()){//判断文件夹是否存在，如果不存在则创建
                    if(!file.getParentFile().exists()){
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                item.transferTo(file);//上传文件
            }
        }


        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "savePic3",method = {RequestMethod.POST})
    public String savePic3(@RequestParam("img2") MultipartFile[] multipartfiles) throws IOException {
        System.out.println("savePic3");
        if(multipartfiles != null && multipartfiles.length>0){
            for(MultipartFile item : multipartfiles)
            { String fileName = item.getOriginalFilename();
                //获取文件名
                String filePath = "E:/picall/a4/" ;
                //自定义上传路径
                File file = new File(filePath,fileName);
                if(!file.exists()){//判断文件夹是否存在，如果不存在则创建
                    if(!file.getParentFile().exists()){
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                item.transferTo(file);//上传文件
            }
        }


        return "index";
    }




        }

